package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.XiangQing;
import com.ysd.util.DBManager;

public class ZXXiangQingDaoImpl {
	public List<XiangQing> getXiangQingsList(){
		List<XiangQing> zxxiangqingList = new ArrayList<XiangQing>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from zxxiangqing");
			while (rs.next()) {
				int pId = rs.getInt("zXXiangQingID");
				String pName = rs.getString("zXBiaoTi");
				String imgs = rs.getString("zXNeiRong");
				String zxnr=rs.getString("zXBTNeiRong");
				XiangQing xiangqing = new XiangQing(pId, pName, imgs, zxnr);
				zxxiangqingList.add(xiangqing);
			}return zxxiangqingList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 根据资讯详情id删除资讯详情
	 * @param zxId
	 * @return
	 */
	public int delzXXiangQingID(int zXXiangQingID) {
		return DBManager.executeUpdate("DELETE FROM zxxiangqing WHERE zXXiangQingID="+zXXiangQingID);
	}
	/**
	 * 批量删除
	 * @param zXXiangQingIDs
	 * @return
	 */
	public int delzXXiangQingIDs(String zXXiangQingIDs) {
		String sql ="DELETE FROM zxxiangqing WHERE zXXiangQingID in("+zXXiangQingIDs+")";
		System.out.println(sql) ;
		return DBManager.executeUpdate(sql);
	
	}
	
	/**
	 * 多条件查询资讯信息
	 * @param z
	 * @return
	 */
	public List<XiangQing> getXiangQingsList(XiangQing z){
		List<XiangQing> list = new ArrayList<XiangQing>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "SELECT z.*,zx.zXNames FROM zxxiangqing z,zxfenlei zx WHERE 1=1";
			
			if (z.getzXXiangQingID()!=0) {
				sql+=" AND zXXiangQingID="+z.getzXXiangQingID();
			}
			if (z.getzXBiaoTi()!=null && !z.getzXBiaoTi().equals("")) {
				sql+=" AND zXBiaoTi LIKE '%"+z.getzXBiaoTi()+"%' ";
			}
			if (z.getzXBTNeiRong()!=null && !z.getzXBTNeiRong().equals("")) {
				sql+=" AND zXBTNeiRong LIKE '%"+z.getzXBTNeiRong()+"%' ";
			}
			if (z.getzXNeiRong()!=null && !z.getzXNeiRong().equals("")) {
				sql+=" AND zXNeiRong LIKE '%"+z.getzXNeiRong()+"%' ";
			}
			if (z.getzXFenLeiId()!=0 ) {
				sql+=" AND zXFenLeiId="+z.getzXFenLeiId();
			}
			sql+=" AND z.zXFenLeiId=zx.zXFEbLeiID";
			System.out.println(sql);
			
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int pId = rs.getInt("zXXiangQingID");
				String pName = rs.getString("zXBiaoTi");
				String zxnr=rs.getString("zXBTNeiRong");
				String imgs = rs.getString("zXNeiRong");
				int zXFenLeiId = rs.getInt("zXFenLeiId");
				String zXNames=rs.getString("zXNames");
				XiangQing zxxiangqing = new XiangQing(pId, pName, imgs, zxnr,zXFenLeiId,zXNames);
				list.add(zxxiangqing);
			}return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 多条件查询资讯信息
	 * @param z
	 * @return
	 */
	public List<XiangQing> getXiangQingsList(XiangQing z,int page,int size){
		List<XiangQing> list = new ArrayList<XiangQing>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "SELECT z.*,zx.zXNames FROM zxxiangqing z,zxfenlei zx WHERE 1=1";
			
			if (z.getzXXiangQingID()!=0) {
				sql+=" AND zXXiangQingID="+z.getzXXiangQingID();
			}
			if (z.getzXBiaoTi()!=null && !z.getzXBiaoTi().equals("")) {
				sql+=" AND zXBiaoTi LIKE '%"+z.getzXBiaoTi()+"%' ";
			}
			if (z.getzXBTNeiRong()!=null && !z.getzXBTNeiRong().equals("")) {
				sql+=" AND zXBTNeiRong LIKE '%"+z.getzXBTNeiRong()+"%' ";
			}
			if (z.getzXNeiRong()!=null && !z.getzXNeiRong().equals("")) {
				sql+=" AND zXNeiRong LIKE '%"+z.getzXNeiRong()+"%' ";
			}
			if (z.getzXFenLeiId()!=0 ) {
				sql+=" AND zXFenLeiId="+z.getzXFenLeiId();
			}
			sql+=" AND z.zXFenLeiId=zx.zXFEbLeiID";
			if(z.getzXNames()!=null && z.getzXNames()!="") {
				sql+=" and zx.zXNames='"+z.getzXNames()+"'";
			}
			sql+=" limit "+(page-1)*2+","+size+"";
			System.out.println(sql);
			
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int pId = rs.getInt("zXXiangQingID");
				String pName = rs.getString("zXBiaoTi");
				String zxnr=rs.getString("zXBTNeiRong");
				String imgs = rs.getString("zXNeiRong");
				int zXFenLeiId = rs.getInt("zXFenLeiId");
				String zXNames=rs.getString("zXNames");
				XiangQing zxxiangqing = new XiangQing(pId, pName, imgs, zxnr,zXFenLeiId,zXNames);
				list.add(zxxiangqing);
			}return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 多条件查询资讯信息
	 * @param z
	 * @return
	 */
	public List<XiangQing> getXiangQingSize(XiangQing z){
		List<XiangQing> list = new ArrayList<XiangQing>();
		
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			String sql = "SELECT z.*,zx.zXNames FROM zxxiangqing z,zxfenlei zx WHERE 1=1";
			
			if (z.getzXXiangQingID()!=0) {
				sql+=" AND zXXiangQingID="+z.getzXXiangQingID();
			}
			if (z.getzXBiaoTi()!=null && !z.getzXBiaoTi().equals("")) {
				sql+=" AND zXBiaoTi LIKE '%"+z.getzXBiaoTi()+"%' ";
			}
			if (z.getzXBTNeiRong()!=null && !z.getzXBTNeiRong().equals("")) {
				sql+=" AND zXBTNeiRong LIKE '%"+z.getzXBTNeiRong()+"%' ";
			}
			if (z.getzXNeiRong()!=null && !z.getzXNeiRong().equals("")) {
				sql+=" AND zXNeiRong LIKE '%"+z.getzXNeiRong()+"%' ";
			}
			if (z.getzXFenLeiId()!=0 ) {
				sql+=" AND zXFenLeiId="+z.getzXFenLeiId();
			}
			sql+=" AND z.zXFenLeiId=zx.zXFEbLeiID";
			System.out.println(sql);
			
			rs=st.executeQuery(sql);
			while(rs.next()) {
				int pId = rs.getInt("zXXiangQingID");
				String pName = rs.getString("zXBiaoTi");
				String zxnr=rs.getString("zXBTNeiRong");
				String imgs = rs.getString("zXNeiRong");
				int zXFenLeiId = rs.getInt("zXFenLeiId");
				String zXNames=rs.getString("zXNames");
				XiangQing zxxiangqing = new XiangQing(pId, pName, imgs, zxnr,zXFenLeiId,zXNames);
				list.add(zxxiangqing);
			}return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 添加资讯详情
	 * @param zxxq
	 * @return
	 */
	public int addziXunXiangQing(XiangQing zxxq) {
		String sql="INSERT INTO zxxiangqing VALUES(NULL, '"+zxxq.getzXBiaoTi()+"','"+zxxq.getzXBTNeiRong()+"','"+zxxq.getzXNeiRong()+"','"+zxxq.getzXFenLeiId()+"')";
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
		
	}
	/**
	 * 根据资讯详情id查询资讯详情
	 * @param Id
	 * @return
	 */
	public XiangQing getZiXunXiangQingID(int Id) {
		XiangQing zx=null;
		Connection conn = DBManager.getConnection();
		Statement st = null;
		ResultSet rs = null;
		
		try {
			st = conn.createStatement();
			rs = st.executeQuery("SELECT * FROM zxxiangqing WHERE zXXiangQingID="+Id);
			while (rs.next()) {
				int pId = rs.getInt(1);
				String pName = rs.getString(2);
				String imgs = rs.getString(3);
				String zxnr=rs.getString(4);
				int zXFenLeiId = rs.getInt(5);
				 zx = new XiangQing(pId, pName, imgs, zxnr,zXFenLeiId);
				
			}return zx;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int xiuGaiZiXun (XiangQing xgzx) {
		String sql="UPDATE zxxiangqing SET zXBiaoTi='"+xgzx.getzXBiaoTi()+"',zXBTNeiRong='"+xgzx.getzXBTNeiRong()+"',zXNeiRong='"+xgzx.getzXNeiRong()+"',zXFenLeiId="+xgzx.getzXFenLeiId()+" WHERE zXXiangQingID="+xgzx.getzXXiangQingID();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
}
